﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using InHD.Properties;
using System.Text.RegularExpressions;
using InHD.utilities;

namespace InHD.model
{
    public class Staff : IDataErrorInfo
    {
        #region Creation
        public static Staff CreateNewStaff()
        {
            return new Staff();
        }

        public static Staff CreateStaff(
            StaffTypes stype,
            string name,
            string fullname,
            string password,
            string address,
            string email,
            string phone,
            string code,
            string unk)
        {
            return new Staff
            {
                SType = stype,
                Username = name,
                Password = password,
                Fullname = fullname,
                Address = address,
                Email = email,
                Phone = phone,
                StaffCode = code,
                StaffUNK = unk,
                LastAccess =  DateTime.Now
            };
        }

        protected Staff()
        {
        }

        #endregion // Creation

        #region State Properties

        /// <summary>
        /// Gets/sets the e-mail address for the Staff.
        /// </summary>
        public string Email { get; set; }

        /// <summary>
        /// Gets/sets the Staff's name.
        /// </summary>
        public string Username { get; set; }

        public string StaffCode { get; set; }

        public string StaffUNK { get; set; }

        /// <summary>
        /// Gets/sets the Staff's fullname.
        /// </summary>
        public string Fullname { get; set; }

        /// <summary>
        /// Gets/sets the Staff's name.
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// Gets/sets the Staff's address.
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// Gets/sets the Staff's phone.
        /// </summary>
        public string Phone { get; set; }

        /// <summary>
        /// Gets/sets the Staff's tax code.
        /// </summary>
        public StaffTypes SType { get; set; }

        public DateTime LastAccess { get; set; }

        public List<string> Fields { get { return new List<string>() { "Fullname", "Address", "Phone", "Email" }; } }

        #endregion // State Properties

        #region IDataErrorInfo Members

        string IDataErrorInfo.Error { get { return null; } }

        string IDataErrorInfo.this[string propertyName]
        {
            get { return this.GetValidationError(propertyName); }
        }

        #endregion // IDataErrorInfo Members

        #region Validation

        /// <summary>
        /// Returns true if this object has no validation errors.
        /// </summary>
        public bool IsValid
        {
            get
            {
                foreach (string property in ValidatedProperties)
                    if (GetValidationError(property) != null)
                        return false;

                return true;
            }
        }

        /// <summary>
        /// Returns true if this object has no validation errors.
        /// </summary>
        public bool IsLoginValid
        {
            get
            {
                foreach (string property in ValidatedLoginProperties)
                    if (GetValidationError(property) != null)
                        return false;

                return true;
            }
        }

        static readonly string[] ValidatedLoginProperties = 
        { 
            "Username", 
            "Password"
        };

        static readonly string[] ValidatedProperties = 
        { 
            "Username", 
            "Fullname",
            "Phone",
            "Email",
            "StaffCode"
        };

        string GetValidationError(string propertyName)
        {
            if (Array.IndexOf(ValidatedProperties, propertyName) < 0)
                return null;

            string error = null;

            switch (propertyName)
            {
                case "Email":
                    error = this.ValidateEmail();
                    break;

                case "Username":
                    error = this.ValidateUsername();
                    break;

                case "Fullname":
                    error = this.ValidateFullName();
                    break;

                case "Password":
                    error = this.ValidatePassword();
                    break;

                case "Phone":
                    error = this.ValidatePhone();
                    break;

                case "StaffCode":
                    error = this.ValidateCode();
                    break;

                default:
                    Debug.Fail("Unexpected property being validated on Staff: " + propertyName);
                    break;
            }

            return error;
        }

        string ValidateEmail()
        {
            if (!utilities.ValidatingUtilities.ValidateMissing(this.Email))
            {
                if (utilities.ValidatingUtilities.ValidateEmail(this.Email))
                {
                    return Resources.InHD_InvalidEmail;
                }
            }

            return null;
        }

        string ValidateUsername()
        {
            if (utilities.ValidatingUtilities.ValidateMissing(this.Username))
            {
                return Resources.InHD_Error_MissingField;
            }
            else
            {
                if (utilities.ValidatingUtilities.ValidateUsername(this.Username))
                {
                    return Resources.InHD_InvalidUsername;
                }
            }
            return null;
        }

        string ValidateFullName()
        {
            if (utilities.ValidatingUtilities.ValidateMissing(this.Fullname))
            {
                return Resources.InHD_Error_MissingField;
            }
            return null;
        }

        string ValidatePassword()
        {
            if (utilities.ValidatingUtilities.ValidateMissing(this.Password))
            {
                return Resources.InHD_Error_MissingField;
            }
            return null;
        }

        string ValidatePhone()
        {
            if (!utilities.ValidatingUtilities.ValidateMissing(this.Phone))
            {
                if (utilities.ValidatingUtilities.ValidatePhone(this.Phone))
                {
                    return Resources.InHD_InvalidPhone;
                }
            }

            return null;
        }

        string ValidateCode()
        {
            if (utilities.ValidatingUtilities.ValidateMissing(this.StaffCode))
            {
                return Resources.InHD_Error_MissingField;
            }
            else
            {
                if (utilities.ValidatingUtilities.ValidateUsername(this.StaffCode))
                {
                    return Resources.InHD_InvalidUsername;
                }
            }

            return null;
        }

        #endregion // Validation

        public Staff CreateShallowCopy()
        {
            return (Staff)this.MemberwiseClone();
        }
    }
}
